package classes.graphicalEngine
{
	import flash.events.*;
	import flash.geom.*;
	
	import mx.collections.*;
	import mx.controls.Alert;
	
	public class GameCamera
	{
		
		//protected static var instance:GameCamera = null;
		// object position
		public static var position:Point = new Point(320, 240);
		// official position while camera is moving == old destination

		
		// direction that the player object should move to (1 for right/down, -1 for left/up)
		public static var direction:Point = new Point(0, 0);
		// destination that the player object should move to (1 for right/down, -1 for left/up)
		public static  var destination:Point = new Point(320, 240);
		public static var oldDestination:Point = new Point(320,240);
		
		// movement speed of the moving player
		protected static const speed:Number = 125;

		public static var xShifting:Number = 0;
		public static var yShifting:Number = 0;

		// default map dimensions
		protected var defaultMapWidth:Number = GameObjectManager.defaultMapWidth;
		protected var defaultMapHeight:Number = GameObjectManager.defaultMapHeight;
		protected var fullSizeMapWidth:Number = GameObjectManager.fullSizeMapWidth;
		protected var fullSizeMapHeight:Number = GameObjectManager.fullSizeMapHeight;

		
		public function GameCamera()
		{
/*			if ( GameCamera.instance != null )
				throw new Error( "GameCamera : Only one Singleton instance should be instantiated" );
*/
		}

/*		static public function get Instance():GameCamera
		{
			if ( instance == null )
				instance = new GameCamera();
			return instance;
		}
*/
		public function click(event:MouseEvent):void
		{
			// setting camera new destination
			if (event.localX > 3 * defaultMapWidth / 4)
				destination.x = position.x + defaultMapWidth / 4;
			if (event.localX < defaultMapWidth / 4)
				destination.x = position.x - defaultMapWidth / 4; 
			if (event.localY > 3 * defaultMapHeight / 4)
				destination.y = position.y + defaultMapHeight / 4;
			if (event.localY < defaultMapHeight / 4)
				destination.y = position.y - defaultMapWidth / 4; 
			
			// keeping player on the screen
			if (destination.x < 0)
				destination.x = 0;
			if (destination.x > fullSizeMapWidth - defaultMapWidth)
				destination.x = position.x;
			if (destination.y < 0)
				destination.y = 0;
			if (destination.y > fullSizeMapHeight - defaultMapHeight)
				destination.y = position.y;
			
			if (!position.equals(destination))
			{
				setGoodDirection();
				
			}
		}

		public function enterFrame(dt:Number):void
		{
			if (!position.equals(destination))
			{
				GameObjectManager.needToBeRefreshed = true;
				xShifting = direction.x * speed * dt;
				yShifting = direction.y * speed * dt;
				
				if (Math.abs(xShifting) < Math.abs(destination.x - position.x))
				position.x += xShifting;
				else
				{
					xShifting = destination.x - position.x;
					position.x = destination.x;
				}
				
				if (Math.abs(yShifting) < Math.abs(destination.y - position.y))
				position.y += yShifting;
				else
				{
					yShifting = destination.y - position.y;
					position.y = destination.y;
				}
				
				setGoodDirection();
			}
		}

		protected function setGoodDirection():void
		{
			if (destination.x < position.x)
			{
				direction.x = -1;
			}
			else if (destination.x > position.x)
			{
				direction.x = 1;
			}
			else direction.x = 0;
			
			if (destination.y < position.y)
			{
				direction.y = -1;
			}
			else if (destination.y > position.y)
			{
				direction.y = 1;
			}
			else direction.y = 0;
		}
		
		public static function hasMoved():Boolean
		{
			return (!position.equals(destination))
		} 

/*		public function setPosition(p:Point):void
		{
			position = p.clone();
			destination = p.clone();
		}
*/
		public function shutdown():void
		{
		}
	}
}